const FORMAT = require("../../api/util.js")
const API = require("../../api/api.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minDate: new Date().getTime() - 30 * 86400000,
    maxDate: new Date().getTime(),
    today: new Date().getTime(),
    huatiList: [{
      tiMu: "今天没有话题",
      agree: 0,
      disagree: 0,
      date: "",
      person: [],
      pinLun: []
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '每日话题'
    })
    const that = this
    let today = FORMAT.formatDay(this.data.today / 1000)
    let userInfo = wx.getStorageSync('userinfo')
    API.getHuatiByDay(today).then(async res => {
      if (res.length != 0) {
        this.setData({
          huatiList: res
        })
        await API.getCommentStatus(res[0]._id).then(res => {
          
          if (res.list.length != 0) {
            this.setData({
              isComment: true
            })
          } else {
            this.setData({
              isComment: false
            })
          }
        })
      } else {
        let huatiList = [{
          tiMu: "今天没有话题",
          agree: 0,
          disagree: 0,
          date: "",
          pinLun: []
        }]
        this.setData({
          huatiList: huatiList
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    let newHuaTi = wx.getStorageSync('newHuaTi')
    if(newHuaTi.length !=0){
      that.setData({
        huatiList:newHuaTi
      })
      wx.removeStorageSync("newHuaTi")
    }
  },

  onSelectFunc: async function (e) {
    
    let selectDay = FORMAT.formatDay(e.detail / 1000)
    let userInfo = wx.getStorageSync('userinfo')
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    await API.getHuatiByDay(selectDay).then(async res => {
      
      if (res.length != 0) {
        this.setData({
          huatiList: res
        })
        await API.getCommentStatus(res[0]._id).then(res => {
          
          if (res.list.length != 0) {
            this.setData({
              isComment: true
            })
          } else {
            this.setData({
              isComment: false
            })
          }
        })
      } else {
        let huatiList = [{
          tiMu: "今天没有话题",
          agree: 0,
          disagree: 0,
          date: "",
          person: [],
          pinLun: []
        }]
        this.setData({
          huatiList: huatiList,
          isComment: true
        })
      }

    })
    wx.hideLoading()
  },

  onAgreeFunc: function (e) {
    
    let item = e.currentTarget.dataset.value
    let data = {
      huaTiId: item._id
    }
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    API.tianJiaShuJu("huaticomment", data).then(res => {
      API.updateHuaTiNumber("agree", item._id).then(res => {
        item.agree += 1
        let arr = []
        arr.push(item)
        this.setData({
          huatiList: arr,
          isComment: true
        })
        wx.hideLoading()
      })
    })
  },

  onDisagreeFunc: function (e) {
    
    let item = e.currentTarget.dataset.value
    let data = {
      huaTiId: item._id
    }
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    API.tianJiaShuJu("huaticomment", data).then(res => {
      API.updateHuaTiNumber("disagree", item._id).then(res => {
        item.disagree += 1
        let arr = []
        arr.push(item)
        this.setData({
          huatiList: arr,
          isComment: true
        })
        wx.hideLoading()
      })
    })
  },

  onCheckComment:function(e){
    
    let item = e.currentTarget.dataset.value
    wx.navigateTo({
      url: "/pages/wentieverydaydetail/wentieverydaydetail?id=" + item._id,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index],
      path: '/pages/index/index'
    }
  },
  onShareTimeline: function () {
let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index]
    }
  }
})